home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / PRINTDIA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.6 KB  |  468 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.8  $
  6. //
  7. // Implementation of class TPrintDialog, a Print and PrintSetup common Dialog
  8. // encapsulation
  9. //----------------------------------------------------------------------------
  10. #pragma hdrignore SECTION
  11. #include <owl/pch.h>
  12. #if !defined(OWL_PRINTDIA_H)
  13. # include <owl/printdia.h>
  14. #endif
  15. #if !defined(OWL_DC_H)
  16. # include <owl/dc.h>
  17. #endif
  18.  
  19. OWL_DIAGINFO;
  20.  
  21. #if !defined(SECTION) || SECTION == 1
  22.  
  23. DEFINE_RESPONSE_TABLE1(TPrintDialog, TCommonDialog)
  24. END_RESPONSE_TABLE;
  25.  
  26. //
  27. //
  28. //
  29. TPrintDialog::TPrintDialog(TWindow*        parent,
  30.                            TData&          data,
  31.                            const char far* printTemplateName,
  32.                            const char far* setupTemplateName,
  33.                            const char far* title,
  34.                            TModule*        module)
  35. :
  36.   TCommonDialog(parent, title, module),
  37.   Data(data)
  38. {
  39.   memset(&Pd, 0, sizeof Pd);
  40.   Pd.lStructSize = sizeof Pd;
  41.   Pd.hwndOwner = Parent ? Parent->GetHandle() : 0;
  42.   Pd.hInstance = *GetModule();
  43.   Pd.Flags = PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK | Data.Flags;
  44.   Pd.Flags &= ~PD_RETURNDEFAULT;
  45.  
  46.   if (printTemplateName) {
  47.     Pd.lpPrintTemplateName = printTemplateName;
  48.     Pd.Flags |= PD_ENABLEPRINTTEMPLATE;
  49.   }
  50.   else
  51.     Pd.Flags &= ~PD_ENABLEPRINTTEMPLATE;
  52.  
  53.   if (setupTemplateName) {
  54.     Pd.lpSetupTemplateName = setupTemplateName;
  55.     Pd.Flags |= PD_ENABLESETUPTEMPLATE;
  56.   }
  57.   else
  58.     Pd.Flags &= ~PD_ENABLESETUPTEMPLATE;
  59.  
  60.   Pd.lpfnPrintHook = 0;
  61.   Pd.lpfnSetupHook = 0;
  62.  
  63.   Pd.nFromPage = (uint16)Data.FromPage;
  64.   Pd.nToPage = (uint16)Data.ToPage;
  65.   Pd.nMinPage = (uint16)Data.MinPage;
  66.   Pd.nMaxPage = (uint16)Data.MaxPage;
  67.   Pd.nCopies = (uint16)Data.Copies;
  68.  
  69. #if defined(BI_PLAT_WIN32)  
  70.   memset(&Psd, 0, sizeof Psd);
  71.   Psd.lStructSize = sizeof Psd;
  72.   Psd.hwndOwner = Parent ? Parent->GetHandle() : 0;
  73.   Psd.hInstance = *GetModule();
  74.   Psd.Flags = PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGEPAINTHOOK | Data.PageSetupFlags;
  75.  
  76.   if (setupTemplateName) {
  77.     Psd.lpPageSetupTemplateName = setupTemplateName;
  78.     Psd.Flags |= PSD_ENABLEPAGESETUPTEMPLATE;
  79.   }
  80.   else
  81.     Psd.Flags &= ~PSD_ENABLEPAGESETUPTEMPLATE;
  82.  
  83.   Psd.lpfnPageSetupHook = 0;
  84.   Psd.lpfnPagePaintHook = 0;
  85.  
  86.   Psd.ptPaperSize = Data.PaperSize;
  87.   Psd.rtMinMargin = Data.MinMargin;
  88.   Psd.rtMargin = Data.Margin;
  89. #endif
  90. }
  91.  
  92. //
  93. //
  94. //
  95. bool
  96. TPrintDialog::DialogFunction(uint msg, TParam1 param1, TParam2 param2)
  97. {
  98.   return TCommonDialog::DialogFunction(msg, param1, param2);
  99. }
  100.  
  101. //
  102. //
  103. //
  104. int
  105. TPrintDialog::DoExecute()
  106. {
  107. #if defined(BI_PLAT_WIN32)  
  108.   if (Data.DoPageSetup) {
  109.     Psd.lpfnPageSetupHook = LPPAGESETUPHOOK(StdDlgProc);
  110.     Psd.lpfnPagePaintHook = LPPAGEPAINTHOOK(StdDlgProc);
  111.  
  112.     Data.Unlock();
  113.  
  114.     Psd.hDevMode = Data.HDevMode;
  115.     Psd.hDevNames = Data.HDevNames;
  116.  
  117.     int ret = ::PageSetupDlg(&Psd);
  118.     if (ret) {
  119.       Data.PageSetupFlags = Psd.Flags;
  120.       Data.Error = 0;
  121.       Data.PaperSize = Psd.ptPaperSize;
  122.       Data.MinMargin = Psd.rtMinMargin;
  123.       Data.Margin = Psd.rtMargin;
  124.     }
  125.     else {
  126.       Data.Error = ::CommDlgExtendedError();
  127.     }
  128.  
  129.     Data.HDevMode = Psd.hDevMode;
  130.     Data.HDevNames = Psd.hDevNames;
  131.  
  132.     Data.Lock();
  133.  
  134.     return ret ? IDOK : IDCANCEL;
  135.   }
  136. #endif
  137.  
  138.   Pd.lpfnPrintHook = LPPRINTHOOKPROC(StdDlgProc);
  139.   Pd.lpfnSetupHook = LPSETUPHOOKPROC(StdDlgProc);
  140.  
  141.   Data.Unlock();
  142.   Pd.hDevMode = Data.HDevMode;
  143.   Pd.hDevNames = Data.HDevNames;
  144.   int ret = ::PrintDlg(&Pd);
  145.   if (ret) {
  146.     Data.Flags = Pd.Flags;
  147.     Data.Error = 0;
  148.     Data.HDc = Pd.hDC;
  149.     Data.FromPage = Pd.nFromPage;
  150.     Data.ToPage = Pd.nToPage;
  151.     Data.Copies = Pd.nCopies;
  152.   }
  153.   else {
  154.     Data.Error = ::CommDlgExtendedError();
  155.   }
  156.   Data.HDevMode = Pd.hDevMode;
  157.   Data.HDevNames = Pd.hDevNames;
  158.   Data.Lock();
  159.   return ret ? IDOK : IDCANCEL;
  160. }
  161.  
  162. //
  163. //
  164. //
  165. bool
  166. TPrintDialog::GetDefaultPrinter()
  167. {
  168.   Pd.Flags |= PD_RETURNDEFAULT;
  169.   Data.ClearDevMode();
  170.   Data.ClearDevNames();
  171.   return DoExecute() == IDOK;
  172. }
  173.  
  174. //----------------------------------------------------------------------------
  175.  
  176. //
  177. //
  178. //
  179. TPrintDialog::TData::TData()
  180. :
  181.   Flags(PD_ALLPAGES|PD_COLLATE),
  182.   Error(0),
  183.   FromPage(-1), ToPage(-1),
  184.   MinPage(-1), MaxPage(-1),
  185.   Copies(1),
  186.   HDevMode(0), DevMode(0),
  187.   HDevNames(0), DevNames(0),
  188.   HDc(0)
  189. #if defined(BI_PLAT_WIN32)  
  190.  ,PageSetupFlags(PSD_DEFAULTMINMARGINS),
  191.   DoPageSetup(false)
  192. #endif
  193. {
  194. }
  195.  
  196. //
  197. //
  198. //
  199. TPrintDialog::TData::~TData()
  200. {
  201.   if (HDevMode) {
  202.     ::GlobalUnlock(HDevMode);
  203.     ::GlobalFree(HDevMode);
  204.   }
  205.   if (HDevNames) {
  206.     ::GlobalUnlock(HDevNames);
  207.     ::GlobalFree(HDevNames);
  208.   }
  209.   if (HDc)
  210.     ::DeleteDC(HDc);
  211. }
  212.  
  213. //
  214. //
  215. //
  216. void
  217. TPrintDialog::TData::Lock()
  218. {
  219.   if (HDevMode)
  220.     DevMode = (DEVMODE far*)::GlobalLock(HDevMode);
  221.   else
  222.     DevMode = 0;
  223.   if (HDevNames)
  224.     DevNames = (DEVNAMES far*)::GlobalLock(HDevNames);
  225.   else
  226.     DevNames = 0;
  227. }
  228.  
  229. //
  230. //
  231. //
  232. void
  233. TPrintDialog::TData::Unlock()
  234. {
  235.   if (HDevMode) {
  236.     ::GlobalUnlock(HDevMode);
  237.     DevMode = 0;
  238.   }
  239.   if (HDevNames) {
  240.     ::GlobalUnlock(HDevNames);
  241.     DevNames = 0;
  242.   }
  243.   if (HDc) {
  244.     ::DeleteDC(HDc);
  245.     HDc = 0;
  246.   }
  247. }
  248.  
  249. //
  250. //
  251. //
  252. void
  253. TPrintDialog::TData::ClearDevMode()
  254. {
  255.   if (HDevMode) {
  256.     ::GlobalUnlock(HDevMode);
  257.     ::GlobalFree(HDevMode);
  258.     HDevMode = 0;
  259.     DevMode = 0;
  260.   }
  261. }
  262.  
  263. //
  264. //
  265. //
  266. void
  267. TPrintDialog::TData::SetDevMode(const DEVMODE far* devMode)
  268. {
  269.   ClearDevMode();
  270.   if (devMode) {
  271.  
  272. #if 0     
  273.     int size = sizeof(DEVMODE) + devMode->dmDriverExtra;
  274. #else
  275.     int size = devMode->dmSize + devMode->dmDriverExtra;
  276. #endif
  277.     HDevMode = ::GlobalAlloc(GHND, size);
  278.     DevMode = (DEVMODE far*)::GlobalLock(HDevMode);
  279.     memcpy(DevMode, devMode, size);
  280.   }
  281. }
  282.  
  283. //
  284. //
  285. //
  286. void
  287. TPrintDialog::TData::ClearDevNames()
  288. {
  289.   if (HDevNames) {
  290.     ::GlobalUnlock(HDevNames);
  291.     ::GlobalFree(HDevNames);
  292.     HDevNames = 0;
  293.     DevNames = 0;
  294.   }
  295. }
  296.  
  297. //
  298. //
  299. //
  300. const char far*
  301. TPrintDialog::TData::GetDriverName() const
  302. {
  303.   return DevNames ? (char far*)DevNames + DevNames->wDriverOffset : 0;
  304. }
  305.  
  306. //
  307. //
  308. //
  309. const char far*
  310. TPrintDialog::TData::GetDeviceName() const
  311. {
  312.   return DevNames ? (char far*)DevNames + DevNames->wDeviceOffset : 0;
  313. }
  314.  
  315. //
  316. //
  317. //
  318. const char far*
  319. TPrintDialog::TData::GetOutputName() const
  320. {
  321.   return DevNames ? (char far*)DevNames + DevNames->wOutputOffset : 0;
  322. }
  323.  
  324. //
  325. //
  326. //
  327. void
  328. TPrintDialog::TData::SetDevNames(const char far* driver,
  329.                                  const char far* device,
  330.                                  const char far* output)
  331. {
  332.   ClearDevNames();
  333.   if (!driver || !device || !output)
  334.     return;
  335.  
  336.   int size1 = strlen(driver)+1;
  337.   int size2 = strlen(device)+1;
  338.   int size3 = strlen(output)+1;
  339.   HDevNames = ::GlobalAlloc(GHND, sizeof(DEVNAMES)+size1+size2+size3);
  340.   DevNames = (DEVNAMES far*)::GlobalLock(HDevNames);
  341.   char far* p = (char far*)(DevNames + 1);
  342.  
  343.   DevNames->wDriverOffset = uint16((char near*)p - (char near*)DevNames);
  344.   if (driver)
  345.     while (*driver)
  346.       *p++ = *driver++;
  347.   *p++ = 0;
  348.  
  349.   DevNames->wDeviceOffset = uint16((char near*)p - (char near*)DevNames);
  350.   if (device)
  351.     while (*device)
  352.       *p++ = *device++;
  353.   *p++ = 0;
  354.  
  355.   DevNames->wOutputOffset = uint16((char near*)p - (char near*)DevNames);
  356.   if (output)
  357.     while (*output)
  358.       *p++ = *output++;
  359.   *p = 0;
  360.  
  361.   DevNames->wDefault = false;
  362. }
  363.  
  364. //
  365. // Pass ownership of our hDC to the caller thru a new's TPrintDC object
  366. //
  367. TPrintDC*
  368. TPrintDialog::TData::TransferDC()
  369. {
  370.   if (!HDc)
  371.     return 0;
  372.   HDC hdc = HDc;
  373.   HDc = 0;
  374.   return new TPrintDC(hdc, AutoDelete);
  375. }
  376.  
  377. #endif
  378. #if !defined(SECTION) || SECTION == 2
  379.  
  380. //
  381. // Read the persistent object from the stream.
  382. //
  383. #pragma warn -par               // version param unused in 16-bit
  384. void*
  385. TPrintDialog::TData::Read(ipstream& is, uint32 version)
  386. {
  387.   is >> Flags;
  388.   is >> FromPage;
  389.   is >> ToPage;
  390.   is >> MinPage;
  391.   is >> MaxPage;
  392.   is >> Copies;
  393.   char far* driver = is.freadString();
  394.   char far* device = is.freadString();
  395.   char far* output = is.freadString();
  396.   uint16 deflt;
  397.   is >> deflt;
  398.   SetDevNames(driver, device, output);
  399.   if (DevNames)
  400.     DevNames->wDefault = deflt;
  401.   delete[] driver;
  402.   delete[] device;
  403.   delete[] output;
  404.  
  405.   int16 size;
  406.   is >> size;
  407.   if (size) {
  408.     DEVMODE far* devMode = (DEVMODE far*)new far char[size];
  409.     is.freadBytes(devMode, size);
  410.     SetDevMode(devMode);
  411.     delete[] devMode;
  412.   }
  413.   else
  414.     ClearDevMode();
  415.  
  416. #if defined(BI_PLAT_WIN32)  
  417.   if (version > 1) {
  418.     is >> PageSetupFlags;
  419.     is >> PaperSize;
  420.     is >> MinMargin;
  421.     is >> Margin;
  422.   }
  423. #endif
  424.  
  425.   return this;
  426. }
  427. #pragma warn .par
  428.  
  429. //
  430. // Write the object to a peristent stream.
  431. //
  432. void
  433. TPrintDialog::TData::Write(opstream& os)
  434. {
  435.   os << Flags;
  436.   os << FromPage;
  437.   os << ToPage;
  438.   os << MinPage;
  439.   os << MaxPage;
  440.   os << Copies;
  441.   os.fwriteString(GetDriverName());
  442.   os.fwriteString(GetDeviceName());
  443.   os.fwriteString(GetOutputName());
  444.   os << (DevNames ? DevNames->wDefault : uint16(0));
  445.  
  446.   if (DevMode) {
  447.  
  448. #if 0 
  449.     int16 size = int16(sizeof(DEVMODE) + DevMode->dmDriverExtra);
  450. #else
  451.     int16 size = int16(DevMode->dmSize + DevMode->dmDriverExtra);
  452. #endif
  453.     os << size;
  454.     os.fwriteBytes(DevMode, size);
  455.   }
  456.   else
  457.     os << int16(0);
  458.  
  459. #if defined(BI_PLAT_WIN32)
  460.   os << PageSetupFlags;
  461.   os << PaperSize;
  462.   os << MinMargin;
  463.   os << Margin;
  464. #endif
  465. }
  466.  
  467. #endif
  468.